package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.clientpackets.C_NPCActionUse;
import l1j.server.server.utils.Random;
import l1j.server.server.utils.SQLUtil;

public class RouletteTable {
	
	private static Logger _log = Logger.getLogger(RouletteTable.class.getName());
	
	private int _Count;
	private int _charId;
	private int _buyCount;
	private int[] _Button;
	private int _Chance;
	private int _Rank;
	private int _Reward;
	private int _TotalBuyCount;
	private int _TotalRewardCount;
	private int _PlayCount;
	private int _ThisNum;

	public int getThisNum(){
		return _ThisNum;
	}
	
	public void setThisNum(int i){
		_ThisNum = i;
	}
	
	public int getTotalBuyCount(){
		return _TotalBuyCount;
	}
	
	public void setTotalBuyCount(int i){
		_TotalBuyCount = i;
	}
	
	public int getTotalRewardCount(){
		return _TotalRewardCount;
	}
	
	public void setTotalRewardCount(int i){
		_TotalRewardCount = i;
	}
	
	public int getPlayCount(){
		return _PlayCount;
	}
	
	public void setPlayCount(int i){
		_PlayCount = i;
	}

	public void setButtonCount(int i, int value){
		if (_Button == null){
			_Button = new int [8];
		}
	    _Button[i] = value;
	}

	public int getButtonCount(int i){
	   return _Button[i];
	}

	public String[] getButtonArray(int pcid){
		loadButtonCount(pcid);
		loadTotalRoulette(pcid);
		int Rewardcount = getReward(); 
		String[] sButton = new String[_Button.length+1];
		for(int i=0; i<_Button.length; i++){
			if (_Button[i] < 10){
				sButton[i] = "350" + _Button[i];
			} else{
				sButton[i] = "35" + _Button[i];
			}
		}
		sButton[8] = " 獎金："+Rewardcount;
		return sButton;
	}
	
	public int getReward(){
		return _Reward;
	}
	
	public void setReward(int i){
		_Reward = i;
	}
	
	public int getChance(){
		return _Chance;
	}
	
	public void setChance(int i){
		_Chance = i;
	}
	
	public int getRank(){
		return _Rank;
	}
	
	public void setRank(int i){
		_Rank = i;
	}
	
	public int getBuyCount(){
		return _buyCount;
	}
	
	public void setBuyCount(int i){
		_buyCount = i;
	}
	
	public int getCharId(){
		return _charId;
	}
	
	public void setCharId(int i){
		_charId = i;
	}
	
	public int getCount(){
		return _Count;
	}
	
	public void setCount(int i){
		_Count = i;
	}
	
	public void loadButtonCount(int charid) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM roulette_button Where charid=?");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			while (rs.next()) {
				int j = 0;
				for (int i=0; i<8; i++){
					setButtonCount(i, rs.getInt("button"+(i+1)));
					j += rs.getInt("button"+(i+1));
					setBuyCount(j);
				}				
				setReward(rs.getInt("reward"));
			}			
		}	
		catch (Exception e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
	}
	public void updateReward(int reward, int charid) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE roulette_button SET reward=? WHERE charid=?");
			pstm.setInt(1, reward);
			pstm.setInt(2, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void updateButtonCount(int ButtonCount , int ButtonNum, int charid) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE roulette_button SET Button"+ButtonNum+"=? WHERE charid=?");
			pstm.setInt(1, ButtonCount);
			pstm.setInt(2, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public int IsPcOnList(int charid) {
		int result = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("SELECT count(*) as cnt FROM roulette_button WHERE charid=?");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			if (rs.next()) {
				result = rs.getInt("cnt");
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return result;
	}
	
	public int IsPcOnTotalList(int charid) {
		int result = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("SELECT count(*) as cnt FROM roulette_total WHERE charid=?");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			if (rs.next()) {
				result = rs.getInt("cnt");
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return result;
	}
	
	public void storeTotalRoulette(int charid) {	
		Connection con = null;	
		PreparedStatement pstm = null;
		try {  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("INSERT INTO roulette_total SET charid=?, datetime = SYSDATE()");
			pstm.setInt(1, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
	}
	
	public void storeRoulette(int charid) {	
		Connection con = null;	
		PreparedStatement pstm = null;
		try {  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("INSERT INTO roulette_button SET charid=?");
			pstm.setInt(1, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
	}
	
	public void deleteRoulette(int charid) {	
		Connection con = null;	
		PreparedStatement pstm = null;
		try {  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("DELETE FROM roulette_button WHERE charid=?");
			pstm.setInt(1, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
	}
	
	public void loadRoulette(int Num) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;		
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM roulette_num Where num=?");
			pstm.setInt(1, Num);
			rs = pstm.executeQuery();
			while (rs.next()) {
				setCount(rs.getInt("count"));
				setRank(rs.getInt("rank"));
				setChance(rs.getInt("chance"));
			}			
		}	
		catch (Exception e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
	}
	
	public void loadTotalRoulette(int charid) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;		
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM roulette_total Where charid=?");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			while (rs.next()) {
				setTotalBuyCount(rs.getInt("totalbuycount"));
				setTotalRewardCount(rs.getInt("totalrewardcount"));
				setPlayCount(rs.getInt("playcount"));
				setThisNum(rs.getInt("thisnum"));
			}			
		}	
		catch (Exception e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
	}
	
	public void updateRouletteCount(int Num, int Count) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE roulette_num SET count=? WHERE num=?");
			pstm.setInt(1, Count);
			pstm.setInt(2, Num);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void updateTotalBuyCount(int charid, int count) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE roulette_total SET totalbuycount=?, playcount=?, datetime = SYSDATE() WHERE charid=?");
			loadTotalRoulette(charid);
			int totalcount = (getTotalBuyCount() + count);
			int playcount = (getPlayCount() + 1);
			pstm.setInt(1, totalcount);
			pstm.setInt(2, playcount);
			pstm.setInt(3, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void updateTotalRewardCount(int charid, int count) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE roulette_total SET totalrewardcount=? WHERE charid=?");
			loadTotalRoulette(charid);
			int totalcount = (getTotalRewardCount() + count);
			pstm.setInt(1, totalcount);
			pstm.setInt(2, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void updateThisNum(int charid, int num) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE roulette_total SET thisnum=? WHERE charid=?");
			pstm.setInt(1, num);
			pstm.setInt(2, charid);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public boolean isCharPlaying(int charid) {
		boolean isPlaying = false;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT thisnum FROM roulette_total WHERE charid=?");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			while (rs.next()) {
				int num = rs.getInt("thisnum");
				if (num != 0){
					isPlaying = true;
				} else {
					isPlaying = false;
				}
			}
		} 
		catch (Exception e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
		return isPlaying;
	}
	
	public int RouletteStart(RouletteTable RT){
		int rnd = Random.nextInt(48) + 74;
		int srnd = Random.nextInt(100) + 1;
		int Num = rnd - 73;				
		if (Num > 24){
			Num -= 24;
		}
		loadRoulette(Num);
		switch (Num){
		case 1:
			if (srnd > 3 && srnd <= 13)
				rnd = 6 + 73;
			else if (srnd > 13 && srnd <= 23)
				rnd = 9 + 73;
			else if (srnd > 23 && srnd <= 33)
				rnd = 12 + 73;
			else if (srnd > 33 && srnd <= 43)
				rnd = 15 + 73;
			else if (srnd > 43 && srnd <= 53)
				rnd = 18 + 73;
			else if (srnd > 53 && srnd <= 63)
				rnd = 21 + 73;
			else if (srnd > 63 && srnd <= 82)
				rnd = 7 + 73;
			else if (srnd > 82)
				rnd = 19 + 73;
			break;
		case 2:
			if (srnd > 12 && srnd <= 22)
				rnd = 6 + 73;
			else if (srnd > 22 && srnd <= 32)
				rnd = 9 + 73;
			else if (srnd > 32 && srnd <= 42)
				rnd = 12 + 73;
			else if (srnd > 42 && srnd <= 52)
				rnd = 15 + 73;
			else if (srnd > 52 && srnd <= 62)
				rnd = 18 + 73;
			else if (srnd > 62 && srnd <= 72)
				rnd = 21 + 73;
			else if (srnd > 72 && srnd <= 86)
				rnd = 7 + 73;
			else if (srnd > 86)
				rnd = 19 + 73;
			break;
		case 3:
			if (srnd > 96)
				rnd = 7 + 73;
			break;			
		case 4:
			if (srnd > 48 && srnd <= 54)
				rnd = 6 + 73;
			else if (srnd > 54 && srnd <= 60)
				rnd = 9 + 73;
			else if (srnd > 60 && srnd <= 66)
				rnd = 12 + 73;
			else if (srnd > 66 && srnd <= 72)
				rnd = 15 + 73;
			else if (srnd > 72 && srnd <= 78)
				rnd = 18 + 73;
			else if (srnd > 78 && srnd <= 84)
				rnd = 21 + 73;
			else if (srnd > 84 && srnd <= 92)
				rnd = 7 + 73;
			else if (srnd > 92)
				rnd = 19 + 73;
			break;
		case 5:
			if (srnd > 48 && srnd <= 54)
				rnd = 6 + 73;
			else if (srnd > 54 && srnd <= 60)
				rnd = 9 + 73;
			else if (srnd > 60 && srnd <= 66)
				rnd = 12 + 73;
			else if (srnd > 66 && srnd <= 72)
				rnd = 15 + 73;
			else if (srnd > 72 && srnd <= 78)
				rnd = 18 + 73;
			else if (srnd > 78 && srnd <= 84)
				rnd = 21 + 73;
			else if (srnd > 84 && srnd <= 92)
				rnd = 7 + 73;
			else if (srnd > 92)
				rnd = 19 + 73;
			break;
		case 8:
			if (srnd > 96)
				rnd = 7 + 73;
			break;
		case 10:
			if (srnd > 72 && srnd <= 76)
				rnd = 6 + 73;
			else if (srnd > 76 && srnd <= 80)
				rnd = 9 + 73;
			else if (srnd > 80 && srnd <= 84)
				rnd = 12 + 73;
			else if (srnd > 84 && srnd <= 88)
				rnd = 15 + 73;
			else if (srnd > 88 && srnd <= 92)
				rnd = 18 + 73;
			else if (srnd > 92 && srnd <= 96)
				rnd = 21 + 73;
			else if (srnd > 96 && srnd <= 98)
				rnd = 7 + 73;
			else if (srnd > 98)
				rnd = 19 + 73;
			break;
		case 11:
			if (srnd > 24 && srnd <= 34)
				rnd = 6 + 73;
			else if (srnd > 34 && srnd <= 44)
				rnd = 9 + 73;
			else if (srnd > 44 && srnd <= 54)
				rnd = 12 + 73;
			else if (srnd > 54 && srnd <= 64)
				rnd = 15 + 73;
			else if (srnd > 64 && srnd <= 74)
				rnd = 18 + 73;
			else if (srnd > 74 && srnd <= 84)
				rnd = 21 + 73;
			else if (srnd > 84 && srnd <= 92)
				rnd = 7 + 73;
			else if (srnd > 92)
				rnd = 19 + 73;
			break;
		case 13:
			if (srnd > 24 && srnd <= 34)
				rnd = 6 + 73;
			else if (srnd > 34 && srnd <= 44)
				rnd = 9 + 73;
			else if (srnd > 44 && srnd <= 54)
				rnd = 12 + 73;
			else if (srnd > 54 && srnd <= 64)
				rnd = 15 + 73;
			else if (srnd > 64 && srnd <= 74)
				rnd = 18 + 73;
			else if (srnd > 74 && srnd <= 84)
				rnd = 21 + 73;
			else if (srnd > 84 && srnd <= 92)
				rnd = 7 + 73;
			else if (srnd > 92)
				rnd = 19 + 73;
			break;
		case 14:
			if (srnd > 96)
				rnd = 19 + 73;
			break;
		case 16:
			if (srnd > 48 && srnd <= 54)
				rnd = 6 + 73;
			else if (srnd > 54 && srnd <= 60)
				rnd = 9 + 73;
			else if (srnd > 60 && srnd <= 66)
				rnd = 12 + 73;
			else if (srnd > 66 && srnd <= 72)
				rnd = 15 + 73;
			else if (srnd > 72 && srnd <= 78)
				rnd = 18 + 73;
			else if (srnd > 78 && srnd <= 84)
				rnd = 21 + 73;
			else if (srnd > 84 && srnd <= 92)
				rnd = 7 + 73;
			else if (srnd > 92)
				rnd = 19 + 73;
			break;
		case 17:
			if (srnd > 36 && srnd <= 42)
				rnd = 6 + 73;
			else if (srnd > 42 && srnd <= 48)
				rnd = 9 + 73;
			else if (srnd > 48 && srnd <= 54)
				rnd = 12 + 73;
			else if (srnd > 54 && srnd <= 60)
				rnd = 15 + 73;
			else if (srnd > 60 && srnd <= 66)
				rnd = 18 + 73;
			else if (srnd > 66 && srnd <= 72)
				rnd = 21 + 73;
			else if (srnd > 72 && srnd <= 86)
				rnd = 7 + 73;
			else if (srnd > 86)
				rnd = 19 + 73;
			break;
		case 20:
			if (srnd > 96)
				rnd = 19 + 73;
			break;
		case 22:
			if (srnd > 72 && srnd <= 76)
				rnd = 6 + 73;
			else if (srnd > 76 && srnd <= 80)
				rnd = 9 + 73;
			else if (srnd > 80 && srnd <= 84)
				rnd = 12 + 73;
			else if (srnd > 84 && srnd <= 88)
				rnd = 15 + 73;
			else if (srnd > 88 && srnd <= 92)
				rnd = 18 + 73;
			else if (srnd > 92 && srnd <= 96)
				rnd = 21 + 73;
			else if (srnd > 96 && srnd <= 98)
				rnd = 7 + 73;
			else if (srnd > 98)
				rnd = 19 + 73;
			break;
		case 23:
			if (srnd > 24 && srnd <= 34)
				rnd = 6 + 73;
			else if (srnd > 34 && srnd <= 44)
				rnd = 9 + 73;
			else if (srnd > 44 && srnd <= 54)
				rnd = 12 + 73;
			else if (srnd > 54 && srnd <= 64)
				rnd = 15 + 73;
			else if (srnd > 64 && srnd <= 74)
				rnd = 18 + 73;
			else if (srnd > 74 && srnd <= 84)
				rnd = 21 + 73;
			else if (srnd > 84 && srnd <= 92)
				rnd = 7 + 73;
			else if (srnd > 92)
				rnd = 19 + 73;
			break;
		case 24:
			if (srnd > 9 && srnd <= 19)
				rnd = 6 + 73;
			else if (srnd > 19 && srnd <= 29)
				rnd = 9 + 73;
			else if (srnd > 29 && srnd <= 39)
				rnd = 12 + 73;
			else if (srnd > 39 && srnd <= 49)
				rnd = 15 + 73;
			else if (srnd > 49 && srnd <= 59)
				rnd = 18 + 73;
			else if (srnd > 59 && srnd <= 69)
				rnd = 21 + 73;
			else if (srnd > 69 && srnd <= 84)
				rnd = 7 + 73;
			else if (srnd > 84)
				rnd = 19 + 73;
			break;			
		}
		int buy = TotalBuy();
		int pay = TotalPay();
		int itemcount =	C_NPCActionUse.GameCount(RT, Num);
		if (pay + itemcount > (buy*0.9))
			return RouletteStart(RT);
		if (rnd < 25)
			return RouletteStart(RT);
		return rnd;
	}
	
	public int TotalBuy() {
		int buy = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;		
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT totalbuycount FROM roulette_total");
			rs = pstm.executeQuery();
			while (rs.next()) {
				buy += rs.getInt("totalbuycount");
			}			
		}	
		catch (Exception e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
		return buy;
	}
	
	public int TotalPay(){
		int pay = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;		
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT totalrewardcount FROM roulette_total");
			rs = pstm.executeQuery();
			while (rs.next()) {
				pay += rs.getInt("totalrewardcount");
			}			
		}	
		catch (Exception e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
		return pay;
	}
}
